﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Input;

namespace WPF_Study
{
    public class LoginVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string property)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(property));
            }
        }

        private readonly Window _window;
        private readonly LoginModel _LoginM = new LoginModel();

        public LoginVm(Window window)
        {
            _window = window;
        }

        public String UserName
        {
            get { return _LoginM.UserName; }
            set
            {
                _LoginM.UserName = value;
                RaisePropertyChanged("UserName");
            }
        }

        public String Password
        {
            get { return _LoginM.Password; }
            set
            {
                _LoginM.Password = value;
                RaisePropertyChanged("Password");
            }
        }

        private void LoginFunc()
        {
            if (UserName == "wpf" && Password == "666")
            {
                Window index = new Index();
                index.Show();
                _window.Close();
            }
            else
            {
                MessageBox.Show("输入的用户名或密码错误");
                UserName = "";
                Password = "";
            }
        }

        private bool CanExecute(object parameter)
        {
            return true;
        }

        // 命令 要绑定的事件处理
        public ICommand LoginCommand
        {
            get { return new RelayCommand(CanExecute, LoginFunc); }
        }
    }
}